package com.yilizhiyou.controller;

import com.yilizhiyou.common.Result;
import com.yilizhiyou.entity.ScenicSpot;
import com.yilizhiyou.model.RecommendationResult;
import com.yilizhiyou.model.UserPreference;
import com.yilizhiyou.service.RecommendationEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/recommend")
@CrossOrigin(origins = "*")
public class RecommendationController {

    @Autowired
    private RecommendationEngine recommendationEngine;

    @Autowired
    private ScenicSpotController scenicSpotController; // 复用其内的模拟数据源

    // 简化版用户偏好入参模型（可直接用UserPreference）
    @PostMapping("/itinerary")
    public Result<RecommendationResult> recommend(@RequestBody(required = false) UserPreference userPreference) {
        try {
            if (userPreference == null) {
                userPreference = new UserPreference("guest");
            }

            // 模拟用户库：只有当前用户作为参照；真实项目应来自DB
            List<UserPreference> allUsers = new ArrayList<>();
            allUsers.add(userPreference);

            // 获取所有景点数据
            List<ScenicSpot> allSpots = scenicSpotController.getAllSpots().getData();
            if (allSpots == null) allSpots = new ArrayList<>();

            RecommendationResult result = recommendationEngine.recommend(userPreference, allUsers, allSpots, 6);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("推荐失败: " + e.getMessage());
        }
    }

    // 便于联调：支持GET快速验证
    @GetMapping("/itinerary")
    public Result<RecommendationResult> recommendViaGet() {
        return recommend(null);
    }
}

